package com.ttdevs.es.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

import com.ttdevs.util.StaticConstants;

public class MainService extends Service implements StaticConstants {
	private boolean isAlarming = false; // is alarming
	private boolean isMonitoring = false; //
	private Handler handler;

	private MainAlertWindowManager alertWindowManager;

	private MainSMSManager smsManager;
	private MainBroadcastManager broadcastManager;
	private MainSensorManager sensorManager;
	private MainAlarmManager alarmManager;

	@Override
	public void onCreate() {
		super.onCreate();

		initData();
	}

	private void initData() {
		handler = new HandlerExtension();

		alertWindowManager = new MainAlertWindowManager(getApplicationContext(), handler);

		smsManager = new MainSMSManager(getApplicationContext(), handler);
		broadcastManager = new MainBroadcastManager(getApplicationContext(), handler);
		sensorManager = new MainSensorManager(getApplicationContext(), handler);
		alarmManager = new MainAlarmManager(getApplicationContext(), handler);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (intent != null && intent.getBooleanExtra(AUTOBOOT, false)) {
			// start from auto boot
			alertWindowManager.addView();
			startAlarm();
		}
		return START_REDELIVER_INTENT;
		// return super.onStartCommand(intent, flags, startId);
	}

	public void start() {
		alertWindowManager.addView();
		startMonitor();
	}

	private void stop() {
		alertWindowManager.removeView();
		if (isAlarming) { // when start alarming,already stop monitor
			stopAlarm();
		} else { // There is no alarm,only to stop monitor
			stopMonitor();
		}
		isAlarming = false;
	}

	private void startAlarm() {
		isAlarming = true;
		alarmManager.start();
		System.out.println("startAlarm ...");
	}

	private void stopAlarm() {
		isAlarming = false;
		alarmManager.stop();
		System.out.println("stopAlarm ...");
	}

	private void startMonitor() {
		isMonitoring = true;
		smsManager.registerContentObserver();
		broadcastManager.registerReceiver();
		sensorManager.registerListener();
	}

	private void stopMonitor() {
		if(isMonitoring){
			smsManager.unregisterContentObserver();
			broadcastManager.unregisterReceiver();
			sensorManager.unregisterListener();
		}
		isMonitoring = false;
	}

	// ***************************************************
	@SuppressLint("HandlerLeak")
	private class HandlerExtension extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case STATE_ALARM:
				stopMonitor();
				startAlarm();
				break;
			case STATE_UNLOCK:
				stop();
				break;
			case STATE_RINGING:
				if (isAlarming) {
					Toast.makeText(MainService.this, "alarming, do nothing...", Toast.LENGTH_LONG).show();
				} else {
					stop();
				}
				break;
			case STATE_IDLE:
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						start();
					}
				}, DELAYSTART);
				break;

			default:
				break;
			}
		}
	}

	// ***********************bind service****************************
	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("onBind...");
		return mBinder;
	}

	private final IBinder mBinder = new ServieBinder();

	public class ServieBinder extends Binder {
		public MainService getService() {
			return MainService.this;
		}
	}
}
